package com.zhoug.viewpermission;

import java.util.List;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;

/**
 * @author: zhoug
 * @since: 2025-06-12
 */
@Dao
public interface MenuDao {
    /**
     * 插入单个菜单
     *
     * @param pageMenu
     */
    @Insert
    void add(NavigationMenu pageMenu);

    /**
     * 插入全部
     *
     * @param menus
     */
    @Insert
    void addAll(List<NavigationMenu> menus);

    /**
     * 删除单个
     *
     * @param pageMenu
     * @return
     */
    @Delete
    void delete(NavigationMenu pageMenu);

    /**
     * 删除单个
     *
     * @param id 主键
     * @return
     */
    @Query("delete from NavigationMenu where id = :id ")
    void deleteById(String id);

    /**
     * 删除 全部
     *
     * @param list
     * @return
     */
    @Delete
    int deleteAll(List<NavigationMenu> list);

    /**
     * 删除 表中的全部数据
     */
    @Query("delete from NavigationMenu")
    void deleteAll();

    /**
     * 查询全部菜单
     *
     * @return
     */
    @Query("select * from NavigationMenu order by sortOrder")
    List<NavigationMenu> getAll();

    /**
     * 根据id查询菜单
     * 之所以用List包装是因为不用的话数据为null时会发生错误,不会有任何回掉,rxjava不会处理null
     *
     * @return
     */
    @Query("select * from NavigationMenu where id = :id and status = 0 and menuType==0")
    List<NavigationMenu> getMenuById(String id);

//    /**
//     * 根据Code查询菜单
//     *
//     * @return
//     */
//    @Query("select * from NavigationMenu where code = :code")
//    List<NavigationMenu> selectByCode(String code);

//    /**
//     * 根据路由查询菜单
//     *
//     * @return
//     */
//    @Query("select * from NavigationMenu where navigation = :navigation")
//    List<NavigationMenu> selectByNavigation(String navigation);

    /**
     * 根据路由查询菜单
     *
     * @return
     */
    @Query("select * from NavigationMenu where path = :path and status = 0 and menuType = 0")
    List<NavigationMenu> getMenuByPath(String path);

    /**
     * 根据上级菜单id查询子菜单
     *
     * @return
     */
    @Query("select * from NavigationMenu where parentId = :parentId and status = 0 and menuType = 0 order by sortOrder")
    List<NavigationMenu> getChildrenByParentId(String parentId);

    /**
     * 查询根节点
     *
     * @return
     */
    @Query("select * from NavigationMenu where parentId is null or parentId = '' and status = 0 order by sortOrder")
    List<NavigationMenu> getRootMenus();

    /**
     * 根据菜单id查询菜单权限
     *
     * @param id
     * @return
     */
    @Query("select * from NavigationMenu where parentId = :id and menuType = 2 and status = 0")
    List<NavigationMenu> getPermissionsByMenuId(String id);

    /**
     * 根据权限标志查询权限
     *
     * @param permissionKey
     * @return
     */
    @Query("select * from NavigationMenu where menuType = 2 and status=0 and parentId in (select id from NavigationMenu where permissionKey= :permissionKey and status=0)")
    List<NavigationMenu> getPermissionsByKey(String permissionKey);




}
